/**
 * 
 */
package nullby.omnia.ui;

import java.util.ArrayList;

import nullby.omnia.ApplicationOmnia;
import nullby.omnia.R;
import nullby.omnia.adapter.AdapterVocWord;
import nullby.omnia.db.ExternalDatabaseHelper;
import nullby.omnia.model.WordOfVoc;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

/**
 * @author Sergey Bandysik (bandysik[at]gmail[dot]com)
 * 
 */
public class EditTextClear extends RelativeLayout implements TextWatcher {

	private ImageButton bOk;
	private ImageButton bClear;
	private AutoCompleteTextView acText;

	private AdapterVocWord adapter;
	// private ArrayList<WordOfVoc> objects;
	// private SQLiteDatabase db;
	// private String DBFile = "ruzh.whd";
	private ExternalDatabaseHelper obj;
	private Listener listener;
	private ArrayList<WordOfVoc> objects;

	public interface Listener {
		void onComplete(ArrayList<WordOfVoc> list);
	}

	/**
	 * @param context
	 */
	public EditTextClear(Context context) {
		super(context);
		initComponents(context);
	}

	public EditTextClear(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initComponents(context);
	}

	public EditTextClear(Context context, AttributeSet attrs) {
		super(context, attrs);
		initComponents(context);
	}

	private void initComponents(Context context) {
		LayoutInflater.from(context).inflate(R.layout.view_edit_text_clear,
				this, true);

		bOk = (ImageButton) findViewById(R.id.bOk);
		bClear = (ImageButton) findViewById(R.id.bClear);
		acText = (AutoCompleteTextView) findViewById(R.id.acText);

		bClear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				acText.setText("");
			}
		});

		acText.addTextChangedListener(this);
		visibilityClear();
	}

	// Set visiblity clear button if
	private void visibilityClear() {
		if (!"".equals(acText.getText().toString())) {
			bClear.setVisibility(View.VISIBLE);
		} else {
			bClear.setVisibility(View.GONE);
		}
	}

	public void setOnOkClickListener(OnClickListener listener) {
		bOk.setOnClickListener(listener);
	}

	public void setListener(Listener listener) {
		this.listener = listener;
	}

	public String getText() {
		return acText.getText().toString();
	}

	public void setString(String text) {
		acText.setText(text);
	}

	public void setAdapter(AdapterVocWord adapter) {
		acText.setAdapter(adapter);
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		visibilityClear();
		obj = ExternalDatabaseHelper
				.newInstanse(ApplicationOmnia.externalVocDbFileName);
		if (obj.databaseFileExists()) {
			objects = obj.getListWord(s.toString(), obj.getReadableDatabase());
			adapter = new AdapterVocWord(getContext(), R.id.tvWord, objects);
			this.setAdapter(adapter);

			listener.onComplete(objects);

			obj.close();
		}
	}
}
